<?php

namespace App\Http\Controllers\Applet\Order;

use App\Exceptions\SponsorException;
use App\Http\Controllers\Applet\Sponsor\BaseSponsorController;
use App\Services\Applet\Order\SponsorOrderService;
use Exception;
use Illuminate\Http\JsonResponse;

class SponsorOrderController extends BaseSponsorController
{
    protected SponsorOrderService $sponsorOrderService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->sponsorOrderService = $this->loadService('Applet\Order\SponsorOrderService');
    }

    /**
     * 创建订单
     * @return JsonResponse
     * @throws SponsorException
     */
    public function create(): JsonResponse
    {
        $rule = [
            'goodsId' => 'required|integer|min:1',
            'quantity' => 'required|integer|min:1',
            'integral' => 'integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $data = $this->sponsorOrderService->create($this->params);
        return outputJsonResult($data);
    }

    /**
     * 购物车创建订单
     * @return JsonResponse
     * @throws SponsorException|Exception
     */
    public function cartCreate(): JsonResponse
    {
        $rule = [
            'cartList' => 'required|array',
            'cartList.*.id' => 'required:integer|min:1',
            'cartList.*.integral' => 'required:integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $data = $this->sponsorOrderService->cartCreate($this->params);
        return outputJsonResult($data);
    }

    /**
     * 取消订单
     * @return JsonResponse
     * @throws Exception
     */
    public function cancel(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->sponsorOrderService->cancel($this->params);
        return outputJsonResult();
    }

    /**
     * 删除订单
     * @return JsonResponse
     * @throws SponsorException
     */
    public function delete(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->sponsorOrderService->delete($this->params);
        return outputJsonResult();
    }

    /**
     * 认养订单列表
     * @return JsonResponse
     * @throws Exception
     */
    public function list(): JsonResponse
    {
        $data = $this->sponsorOrderService->list($this->params);
        return outputJsonResult($data);
    }

    /**
     * 认养订单详情
     * @return JsonResponse
     * @throws Exception
     */
    public function detail(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];
        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $data = $this->sponsorOrderService->detail($this->params);
        return outputJsonResult($data);
    }

}
